package com.test.callable;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * Runnable使用demo
 *
 * @author wangjie
 * @version V1.0
 * @date 2019/12/25
 */
@Slf4j
public class CallableDemo {

    public static void main(String[] args) {

        //main线程开始时间
        Long startTime = System.currentTimeMillis();

        FutureTask<Integer> futureTask = new FutureTask(new CallableResource());
        Thread thread = new Thread(futureTask);
        thread.start();

        int i = 1;

        Integer o = null;
        try {
            log.info("【这是个main线程,后续运算，预计耗时2秒钟】");
            TimeUnit.SECONDS.sleep(3);
            o = futureTask.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("【i+0：】{}",o+i);
        log.info("main总耗时{}ms",(System.currentTimeMillis()-startTime));
    }
}

/**
 * 线程
 */
@Slf4j
class CallableResource  implements Callable<Integer>{


    @Override
    public Integer call() throws Exception {
        log.info("【这是个Callable使用demo,开始运算，预计耗时3秒钟】");
        TimeUnit.SECONDS.sleep(3);
        return 1;
    }
}